package com.qfedu.ssm.services;

import com.qfedu.ssm.dao.ItemDAO;
import com.qfedu.ssm.pojos.Item;
import com.qfedu.ssm.pojos.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ItemService {
    @Autowired
    private ItemDAO itemDAO;
    @Autowired
    private StringRedisTemplate redisTemplate;

    public List<ItemVO> listItems(int userId){

        List<Item> items = itemDAO.listItem();
        List<ItemVO> itemVOS = new ArrayList<ItemVO>();
        for (Item item:items){
            boolean state = false;
            String key = "wzc-redis-zan"+item.getItemId()+"-"+userId;
            boolean b = redisTemplate.hasKey(key);
            if (b){
                String code = redisTemplate.boundValueOps(key).get();
                if ("1".equals(code)){
                    state = true;
                }
            }
            String key2 = "wzc-redis-zan"+item.getItemId();
            int num = redisTemplate.hasKey(key2)?Integer.valueOf(redisTemplate.boundValueOps(key2).get()):0;
            itemVOS.add(new ItemVO(item,state,num));
        }
        return itemVOS;
    }
}
